

package com.jb.demo.tree;

import java.io.PrintStream;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
 

public class DemoModel
    implements TreeModel
{

    private void $init$()
    {
        treeModelListeners = new Vector();
    }

    public DemoModel(Person root)
    {
        $init$();
        showAncestors = false;
        rootPerson = root;
    }

    public void showAncestor(boolean b, Object newRoot)
    {
        showAncestors = b;
        Person oldRoot = rootPerson;
        if(newRoot != null)
            rootPerson = (Person)newRoot;
        fireTreeStructureChanged(oldRoot);
    }

    protected void fireTreeStructureChanged(Person oldRoot)
    {
        TreeModelEvent e = new TreeModelEvent(this, new Object[] {
            oldRoot
        });
        TreeModelListener tml;
        for(Iterator iterator = treeModelListeners.iterator(); iterator.hasNext(); tml.treeStructureChanged(e))
            tml = (TreeModelListener)iterator.next();

    }

    public void addTreeModelListener(TreeModelListener l)
    {
        treeModelListeners.addElement(l);
    }

    public Object getChild(Object parent, int index)
    {
        Person p = (Person)parent;
        if(showAncestors)
        {
            if(index > 0 && p.getFather() != null)
                return p.getMother();
            else
                return p.getFather();
        } else
        {
            return p.getChildAt(index);
        }
    }

    public int getChildCount(Object parent)
    {
        Person p = (Person)parent;
        if(showAncestors)
        {
            int count = 0;
            if(p.getFather() != null)
                count++;
            if(p.getMother() != null)
                count++;
            return count;
        } else
        {
            return p.getChildCount();
        }
    }

    public int getIndexOfChild(Object parent, Object child)
    {
        Person p = (Person)parent;
        if(showAncestors)
        {
            int count = 0;
            Person father = p.getFather();
            if(father != null)
            {
                count++;
                if(father == child)
                    return 0;
            }
            if(p.getMother() != child)
                return count;
            else
                return -1;
        } else
        {
            return p.getIndexOfChild((Person)child);
        }
    }

    public Object getRoot()
    {
        return rootPerson;
    }

    public boolean isLeaf(Object node)
    {
        Person p = (Person)node;
        if(showAncestors)
            return p.getFather() == null && p.getMother() == null;
        else
            return p.getChildCount() == 0;
    }

    public void removeTreeModelListener(TreeModelListener l)
    {
        treeModelListeners.removeElement(l);
    }

    public void valueForPathChanged(TreePath path, Object newValue)
    {
        System.out.println((new StringBuilder()).append("*** valueForPathChanged : ").append(path).append(" --> ").append(newValue).toString());
    }

    private boolean showAncestors;
    private Vector treeModelListeners;
    private Person rootPerson;
}
